class Solution(object):
    def largestOverlap(self, img1, img2):
        """
        :type img1: List[List[int]]
        :type img2: List[List[int]]
        :rtype: int
        """
        n = len(img1)
        max_value = 0
        for i in range(-n + 1, n):
            for j in range(-n + 1, n):
                value = 0
                for t in range(0, n):
                    for q in range(0, n):
                        if 0 <= t + i < n and 0 <= q + j < n and img1[t + i][q + j] & img2[t][q]:
                            value += 1
                max_value = max(max_value, value)
        return max_value


data = Solution()
img1 = [[1, 1, 0], [0, 1, 0], [0, 1, 0]]
img2 = [[0, 0, 0], [0, 1, 1], [0, 0, 1]]
print(data.largestOverlap(img1, img2))
